package com.googlecode.icefusion.ui.commons.navigation;


import java.util.ArrayList;

import javax.faces.context.FacesContext;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ActionEvent;


/**
 * A menu entry for simplified menus, like collapsibleMenu. Use the navigation id in the facelets-config.xml for the
 * action attribute.
 * 
 * @author Rainer Eschen
 * 
 */
public class SimpleMenuItem {

    /**
     * Label for the menu entry
     */
    String label;

    /**
     * NavigationId (you need a corresponding definition in the faces-config.xml)
     */
    String action;

    /**
     * A list of sub menus
     */
    ArrayList<SimpleMenuItem> subMenu = new ArrayList<SimpleMenuItem>();

    /**
     * @return the label
     */
    public String getLabel() {
        return label;
    }

    /**
     * @param label the label to set
     */
    public void setLabel(String label) {
        this.label = label;
    }

    /**
     * @return the action
     */
    public String getAction() {
        return action;
    }

    /**
     * @param action the action to set
     */
    public void setAction(String action) {
        this.action = action;
    }

    /**
     * @return the subMenu
     */
    public ArrayList<SimpleMenuItem> getSubMenu() {
        return subMenu;
    }

    /**
     * @param subMenu the subMenu to set
     */
    public void setSubMenu(ArrayList<SimpleMenuItem> subMenu) {
        this.subMenu = subMenu;
    }

    /**
     * @see javax.faces.event.ActionListener#processAction(javax.faces.event.ActionEvent)
     */
    public void processAction(ActionEvent actionEvent) throws AbortProcessingException {

        FacesContext context = FacesContext.getCurrentInstance();
        context.getApplication().getNavigationHandler().handleNavigation(context, null, this.getAction());
    }

    /**
     * Checks if a sub menu exists.
     * 
     * @return true: sub menu exists
     */
    public Boolean getHasSubMenu() {

        return this.getSubMenu().size() > 0;
    }
}
